package net.oschina.app.fragment.move;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import net.oschina.app.R;
import net.oschina.app.adapter.move.MoveDetailAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean2.MoveCommentBean;
import net.oschina.app.bean2.TweetBean;
import net.oschina.app.protocol.AccessProtocol;
import net.oschina.app.protocol.Constant;
import net.oschina.app.protocol.move.MoveDetailProtocol;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Vashon on 2017/1/17.
 */

public class MoveDetailFragment extends BaseFragment {

    ListView mLvMoveComment;
    TextView mMoveBottom;
//    @BindView(R.id.iv_move_item)
    CircleImageView mIvMoveItem;
//    @BindView(R.id.tv_move_username)
    TextView mTvMoveUsername;
//    @BindView(R.id.tv_move_time)
    TextView mTvMoveTime;
//    @BindView(R.id.tv_move_content)
    TextView mTvMoveContent;
//    @BindView(R.id.tv_comment_count)
    TextView mTvCommentCount;
//    @BindView(R.id.ll_move_content)
    LinearLayout mLlMoveContent;
//    @BindView(R.id.tv_move_comment_likeed)
    TextView mTvMoveCommentLikeed;
    private TweetBean mData;
    private View mHeadView;

    @Override
    public int getLayoutRes() {
        return R.layout.move_detail_list;
    }

    @Override
    public void initView() {
        mHeadView = View.inflate(mActivity, R.layout.move_detail, null);
        mLvMoveComment = (ListView) mRoot.findViewById(R.id.lv_move_comment);
        mMoveBottom = (TextView) mRoot.findViewById(R.id.move_bottom);

        mIvMoveItem = (CircleImageView) mHeadView.findViewById(R.id.iv_move_item);
        mTvMoveUsername = (TextView) mHeadView.findViewById(R.id.tv_move_username);
        mTvMoveTime = (TextView) mHeadView.findViewById(R.id.tv_move_time);
        mTvMoveContent = (TextView) mHeadView.findViewById(R.id.tv_move_content);
        mTvCommentCount = (TextView) mHeadView.findViewById(R.id.tv_comment_count);
        mTvMoveCommentLikeed = (TextView) mHeadView.findViewById(R.id.tv_move_comment_likeed);
//        ButterKnife.bind(this, mHeadView);
        mLvMoveComment.addHeaderView(mHeadView);
        Bundle bundle = getArguments();
        mData = (TweetBean) bundle.getSerializable("data");
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        mTvMoveUsername.setText(mData.getAuthor());
        Spanned spanned = Html.fromHtml(mData.getBody());
        mTvMoveContent.setText(spanned);
        mTvMoveTime.setText(mData.getPubDate());
        mTvCommentCount.setText(Integer.toString(mData.getCommentCount()));
        String imageUrl = (mData).getPortrait();
        if (imageUrl != null) {
            imageUrl = imageUrl.replace("192.168.1.107:8080", Constant.GENYMOTION);
            Glide.with(getActivity()).load(imageUrl).into(mIvMoveItem);
        }
        if (mData.getLikeCount() != 0 && mData.getLikeCount() < 7) {
            String likeUser = mData.getLikeList().get(0).getName();
            for (int i = 1; i < mData.getLikeList().size(); i++) {
                likeUser = likeUser + mData.getLikeList().get(i).getName() + " ";
            }
            likeUser = likeUser + "觉得很赞";
            mTvMoveCommentLikeed.setText(likeUser);
        } else if (mData.getLikeCount() >= 7) {
            String likeUser = mData.getLikeList().get(0).getName();
            for (int i = 1; i < 7; i++) {
                likeUser = likeUser + mData.getLikeList().get(i).getName() + " ";
            }
            likeUser = likeUser + "等" + mData.getLikeCount() + "人觉得很赞";
            mTvMoveCommentLikeed.setText(likeUser);
        }

        final MoveDetailProtocol detailProtocol = new MoveDetailProtocol();

        detailProtocol.setOnAccessNetListener(new AccessProtocol.onAccessNetListener() {
            @Override
            public void onSuccess() {
                MoveCommentBean commentBean = detailProtocol.getBody();
                mLvMoveComment.setAdapter(new MoveDetailAdapter(getActivity(), commentBean.getComments()));
            }

            @Override
            public void onFail() {
                Toast.makeText(mActivity, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        detailProtocol.getData();
    }
}
